class Admin::ApplicationController < ActionController::Base
  before_action :check_admin_user
  helper_method :current_admin_user, :current_admin_user_id

  # 后台登陆用户
  def current_user
    if session[:admin_user_id]
      @admin_user ||= AdminUser.find_by(id: session[:admin_user_id])
    end
  end

  def current_admin_user_id
    current_user.id
  end

  def current_company_id
    current_company.id
  end

  def current_company
    current_user.companies.find_by(used: true)
  end

  # 验证用户是否登陆
  def check_admin_user
   redirect_to admin_sessions_path and return if current_user.blank?
  end

  # 后台控制器用到的文件方法
  def change_img_params(img)
    begin
      Base64.decode64(img) #To check if thats a base64 string
      if img
        img = file_decode(img.split(',')[1],"some file name") #getting only the string leaving out the data/<format>
      end
    rescue Exception => e
      img
    end
  end

end
